package com.cmwa.solution.business.infodisclosure.regulatoryreport.controller;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.infodisclosure.institution.entity.InstitutionVo;
import com.cmwa.solution.business.infodisclosure.institution.service.InstitutionService;
import com.cmwa.solution.business.infodisclosure.regulatoryreport.entity.RegulatoryReportVo;
import com.cmwa.solution.business.infodisclosure.regulatoryreport.service.RegulatoryReportService;
import com.cmwa.solution.business.infodisclosure.report.service.ReportInfoService;
import com.cmwa.solution.business.myHome.todo.service.TodoService;
import com.cmwa.solution.sys.attach.entity.AttachVo;
import com.cmwa.solution.sys.msg.util.StringUtil;
import com.cmwa.solution.utils.cached.DictionaryCached;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/business/regulatoryReport")
public class RegulatoryReportController extends BaseController{
	
	private static final String PRE_VIEW_PATH = "business/infoDisclosure/regulatoryReport";
	
	@Resource
	private RegulatoryReportService regulatoryReportService;
	
	@Resource
	private InstitutionService institutionService;
	
	@Resource
	private TodoService todoService;
	
	@Resource
	private ReportInfoService reportInfoService;
	
	@RequestMapping("/regulatoryReportAdd")
	public String regulatoryReportAdd() {
		return PRE_VIEW_PATH+"/regulatoryReportAdd";
	}
	
	
	@RequestMapping("/addRegulatoryReport")
	public String addRegulatoryReport(RegulatoryReportVo regulatoryReportVo,@RequestParam(value = "regAttIds", required = false) String regAttIds) {
		regulatoryReportService.addRegulatoryReport(regulatoryReportVo, regAttIds);
		return redirectSuccess();
	}
	
	@RequestMapping("/regulatoryReportDownView")
	public String regulatoryReportDownView(SearchParam sp,ModelMap model) {
		model.addAttribute("sp", sp);
		model.addAttribute("instList", regulatoryReportService.getReportInstList());
		return PRE_VIEW_PATH+"/regulatoryReportDown";
	}
	
	@RequestMapping("/regulatoryReportBatchDown")
	public void regulatoryReportBatchDown(SearchParam sp,HttpServletResponse response) {
		OutputStream out = null;
		try {
			response.reset();
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/octet-stream;charset=UTF-8");
			response.setHeader("Content-Disposition",
					"Attachment;filename="+new String("监管报告".getBytes("gb2312"), "ISO8859-1")+".zip");
			out = response.getOutputStream();
			regulatoryReportService.regulatoryReportBatchDown(sp, out);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 监管报告跳转列表页面（简化版）
	 * @return
	 */
	@RequestMapping("/regulatoryReportListNew")
	public String regulatoryReportListNew(SearchParam param,ModelMap model) {
		String jsonStr = "";
		if(param!=null&&!param.getSp().isEmpty()) {
			jsonStr = JSONObject.fromObject(param.getSp()).toString();
		}
		model.addAttribute("parameter", jsonStr);
		model.addAttribute("instList", regulatoryReportService.getReportInstList());
		model.addAttribute("reprotInfoList", reportInfoService.getReportInfoList("SUPERVISE_REPORT"));//报告类型
		model.addAttribute("dicList", DictionaryCached.getDictionaryByRootType("TODO_STATUS"));//状态
		return PRE_VIEW_PATH+"/regulatoryReportListNew";
	}
	/**
	 * 根据条件查询报表列表信息
	 * @param param
	 * @return
	 */
	@RequestMapping("/regulatoryReportListPageNew")
	@ResponseBody
	public Page<RegulatoryReportVo> regulatoryReportListPageNew(SearchParam param) {
		return regulatoryReportService.getRegulatoryReportListPageNew(param);
	}
	/**
	 * 跳转上传附件页面
	 * @param flag
	 * @param param
	 * @param model
	 * @return
	 */
	@RequestMapping("/regulatoryReportAddNew")
	public String regulatoryReportAddNew(@RequestParam(value = "flag", required = false) String flag,SearchParam param,ModelMap model) {
		model.addAttribute("flag", flag);
		param.getSp().put("flag", flag);
		model.addAttribute("regulatoryReport",regulatoryReportService.getRegulatoryReportInfoNew(param));
		List<AttachVo> list = regulatoryReportService.getReportAttInfoNew(param);
		String attachListJson = null;
		if(!CollectionUtils.isEmpty(list)) {
			attachListJson = StringUtil.changeSpecialCharacter(JSONArray.fromObject(list).toString());
		}
		model.addAttribute("attachList",attachListJson);
		return PRE_VIEW_PATH+"/regulatoryReportAddNew";
	}
	/**
	 * 上传附件 报告R 材料M
	 * @param regulatoryReportVo
	 * @param regAttIds
	 * @param flag
	 * @return
	 */
	@RequestMapping("/addRegulatoryReportNew")
	public String addRegulatoryReportNew(RegulatoryReportVo regulatoryReportVo,@RequestParam(value = "regAttIds", required = false) String regAttIds
			,@RequestParam(value = "flag", required = false) String flag) {
		regulatoryReportService.addRegulatoryReportNew(regulatoryReportVo, regAttIds,flag);
		return redirectSuccess();
	}
	/**
	 * 根据条件判断是否存在附件
	 * @param sp
	 * @return
	 */
	@RequestMapping("/judgeHasAttach")
	@ResponseBody
	public boolean judgeHasAttach(SearchParam sp) {
		return CollectionUtils.isEmpty(regulatoryReportService.getReportAttInfoNew(sp));
	}
	/**
	 * 附件下载
	 * @param sp
	 * @param response
	 */
	@RequestMapping("/regulatoryReportDownNew")
	public void regulatoryReportDownNew(SearchParam sp,HttpServletResponse response) {
		OutputStream out = null;
		try {
			String title = "";
			if(StringUtils.equals("true", sp.getSp().get("batch").toString())) {
				title = StringUtils.equals("M", sp.getSp().get("flag").toString()) ? "监管报告材料" : "监管报告文件";
			}else {
				title = sp.getSp().get("instSName").toString()+"_"+sp.getSp().get("reportTypeName").toString()+"_"+sp.getSp().get("reportDate").toString();
			}
			 
			response.reset();
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/octet-stream;charset=UTF-8");
			response.setHeader("Content-Disposition",
					"Attachment;filename="+new String(title.getBytes("gb2312"), "ISO8859-1")+".zip");
			out = response.getOutputStream();
			regulatoryReportService.regulatoryReportDownNew(sp, out);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 根据条件查询监管报告是否存在附件(用于首页监管报告披量下载)
	 * @param param
	 * @return
	 */
	@RequestMapping("/checkRegulatoryReportAtt")
	@ResponseBody
	public Result<String> checkRegulatoryReportAtt(SearchParam param) {
		Result<String> result = new Result<String>(false);
		List<String> ids = regulatoryReportService.getRegulatoryReportIdNew(param);
		if(CollectionUtils.isEmpty(ids)) {
			result.setSuccess(false);
		}else {
			Result<Object> obj = todoService.validateExistsFile("INFO_DIS_REGULATORY", StringUtils.join(ids,","), "output");
			result.setSuccess(obj.isSuccess());
			result.setObject(StringUtils.join(ids,","));
		}
		return result;
	}
	/**
	 * 根据监管机构id查询监管报告类型（用于下拉框联动）
	 * @param instId
	 * @return
	 */
	@RequestMapping("/getReportTypesByInst")
	@ResponseBody
	public List<Map<String, String>> getReportTypesByInst(@RequestParam("instId") String instId,@RequestParam(value="reportName",required = false) String reportName){
		return regulatoryReportService.getReportTypesByInst(instId, reportName);
	}
	
	
	@RequestMapping("/getReportInstList")
	@ResponseBody
	public List<InstitutionVo> getReportInstList(){
		return regulatoryReportService.getReportInstList();
	}
	
}
